/* eslint import/no-unresolved: 0
 , import/no-webpack-loader-syntax: 0
 */
// 本来antd支持按需加载css，但会覆盖掉项目的less设置
require('antd/dist/antd.css');
require('../../css/index.less');

import {
    Route,
    IndexRoute
} from 'react-router';
import React from 'react';
import {Spin} from 'antd';

function lazy(bundle) {
    if (typeof bundle === 'function') {
        return class extends React.Component {
            state = {component: null};

            componentDidMount() {
                bundle(({default: component}) => {
                    this.setState({component});
                });
            }

            render() {
                const Target = this.state.component;
                if (Target) {
                    return <Target {...this.props}/>;
                }
                return <Spin spinning/>;
            }
        };
    }
    return bundle.default;
}

const [
    App,
    NotFound,
    Login,
    Index,
    Dict,
    SpecialDepartment,
    Department,
    Workstation,
    Action,
    Module,
    Role,
    User,
    PrintTemplate,
    PrintTemplateDesign,
    OutPatientPhysician,
    WriteReport
] = process.env.NODE_ENV !== 'production' ? [
    require('./app'),
    require('./404'),
    require('./login'),
    require('./index'),
    require('./dict'),
    require('./specialDepartment'),
    require('./department'),
    require('./workstation'),
    require('./action'),
    require('./module'),
    require('./role'),
    require('./user'),
    require('./printTemplate'),
    require('./printTemplate/design'),
    require('./outpatientPhysician'),
    require('./writeReport')
] : [
    require('bundle?lazy!./app'),
    require('bundle?lazy!./404'),
    require('bundle?lazy!./login'),
    require('bundle?lazy!./index'),
    require('bundle?lazy!./dict'),
    require('bundle?lazy!./specialDepartment'),
    require('bundle?lazy!./department'),
    require('bundle?lazy!./workstation'),
    require('bundle?lazy!./action'),
    require('bundle?lazy!./module'),
    require('bundle?lazy!./role'),
    require('bundle?lazy!./user'),
    require('bundle?lazy!./printTemplate'),
    require('bundle?lazy!./printTemplate/design'),
    require('bundle?lazy!./outpatientPhysician'),
    require('bundle?lazy!./writeReport')
];

// 路由定义
export default (
    <Route path="/" component={lazy(App)}>
        <IndexRoute component={lazy(Index)}/>
        <Route path="login" component={lazy(Login)}/>
        <Route component={lazy(Index)}>
            <Route path="dict" component={lazy(Dict)}>
                <Route path=":type" component={lazy(Dict)}/>
            </Route>
            <Route path="outpatientPhysician" component={lazy(OutPatientPhysician)}>
                <Route path="myPatient" component={lazy(OutPatientPhysician)}/>
            </Route>
            <Route path="writeReport/:id" component={lazy(WriteReport)}/>
            <Route path="specialDepartment" component={lazy(SpecialDepartment)}/>
            <Route path="department" component={lazy(Department)}/>
            <Route path="workstation" component={lazy(Workstation)}/>
            <Route path="action" component={lazy(Action)}/>
            <Route path="module" component={lazy(Module)}/>
            <Route path="role" component={lazy(Role)}/>
            <Route path="user" component={lazy(User)}/>
            <Route path="printTemplate" component={lazy(PrintTemplate)}/>
            <Route path="printTemplate/design/:id" component={lazy(PrintTemplateDesign)}/>
            <Route path="*" component={lazy(NotFound)}/>
        </Route>
    </Route>
);
